package com.offcn.utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author qf_meng
 * @create 2021-07-06 17:26
 */
@WebServlet(name = "BaseServlet", urlPatterns = "/BaseServlet")
public class BaseServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request.setCharacterEncoding("utf-8");
        //获取way的值：way的为doLogin的话我们调用doLogin方法处理登录请求
        //way的为doRegiser的话我们调用doRegiser方法处理注册请求
        String way = request.getParameter("way");
        //根据way的值使用反射的方式自动去寻找way值对应的方法
        try {
            //way是方法的名字，后边两个参数是way方法中的两个参数类型
            Method declaredMethod = this.getClass().getDeclaredMethod(way, HttpServletRequest.class, HttpServletResponse.class);
            declaredMethod.setAccessible(true);//取消私有属性的访问权限
            //方法的调用
            declaredMethod.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
